package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.RealUserListDto;
import com.heima.model.user.pojos.RealUser;
import com.heima.user.mapper.RealUserMapper;
import com.heima.user.service.RealUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author ybw
 * @version 1.0
 * @date 2021/12/22 14:07
 */
@Service
@Transactional
public class RealUserServiceImpl extends ServiceImpl<RealUserMapper, RealUser>
    implements RealUserService {

  @Autowired private RealUserMapper realUserMapper;
  /**
   * 用户审核列表
   *
   * @param dto
   * @return
   */
  @Override
  public ResponseResult authUserList(RealUserListDto dto) {
    // 校验参数
    if (ObjectUtils.isEmpty(dto)) {
      return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }
    // 获取分页参数
    Integer postPage = dto.getPage();
    Integer postSize = dto.getSize();
    // 查询所有
    Page<RealUser> page = new Page<>(postPage, postSize);
    LambdaQueryWrapper<RealUser> qw = new LambdaQueryWrapper<>();
    Page<RealUser> realUserPage = realUserMapper.selectPage(page, qw);
    List<RealUser> realUserList = realUserPage.getRecords();
    return ResponseResult.okResult(realUserList);
  }

  /**
   * 人工审核
   *
   * @param dto
   * @param type
   * @return
   */
  @Override
  public ResponseResult auth(RealUserListDto dto, Integer type) {
    // 参数校验
    if (ObjectUtils.isEmpty(dto)) {
      return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }
    // 获取用户id
    Integer postId = dto.getId();
    if (postId == null) {
      return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
    }
    // 根据id查询用户信息
    RealUser realUser = realUserMapper.selectById(postId);
    if (ObjectUtils.isEmpty(realUser)) {
      return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
    }
    // 获取用户状态
    Short status = realUser.getStatus();
    if (status != 1) {
      // 不在待审核状态
      return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }
    realUser.setStatus((short) (type == 1 ? 9 : 2));
    realUserMapper.updateById(realUser);
    return ResponseResult.okResult(200, "操作成功");
  }
}
